/* ======================================================================================== *\

		FILE:				ScreensaverMode.cpp
		AUTHOR:				Jack Hoxley

		DESCRIPTION:		Implements the command-line parsing to determine what
							screensaver characteristics should be used by the application

		CHANGE:				6th May 2007
								Basic functionality implemented.

\* ======================================================================================== */

// Include Standard Headers
// ------------------------
#include "DXUT.h"

// Include project headers
// -----------------------
#include "Profiling.h"
#include "Utils.h"
#include "ScreensaverMode.h"



namespace ScrnSv
{
	// =================================
	// P R I V A T E   V A R I A B L E S
	// =================================
	bool g_bPreviewMode;
	HWND g_hPreviewWindow;

	bool g_bScreensaverMode;

	bool g_bConfigurationMode;
	HWND g_hConfigParentWindow;

	// ===============================
	// P U B L I C   F U N C T I O N S
	// ===============================
	void ParseApplicationCommandLine( )
	{
		ENTER;

		LPWSTR *szArglist;
		int nArgs;

		szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
		if( NULL == szArglist )
		{
			ERR_OUT( L"Unable to convert and parse application's command line!" );
			return;
		}

		/* 
			First argument is always the name of the executable

			Second argument is:
				/p for preview
				Then third argument is decimal HWND to preview within

			Second argument is:
				/c:NNNNN where NNNN is the decimal HWND for the parent of the configuration window

			Second argument is:
				/s for regular screensaver mode
		*/

		// Reset the internal flags:
		g_bPreviewMode			= false;
		g_bScreensaverMode		= false;
		g_bConfigurationMode	= false;

		if( nArgs >= 2 )
		{
			WCHAR* wcSecondArg = szArglist[1];

			if( 0 == _wcsicmp( wcSecondArg, L"/p" ) )
			{
				// Preview mode has been requested
				g_bPreviewMode = true;

				// TODO: Parse remainder of argument for
				// the HWND to render into.
				if( nArgs >= 3 )
				{
					g_hPreviewWindow = (HWND)((DWORD_PTR)_wtoi( szArglist[2] ) );
				}
			}
			else if( 0 == _wcsicmp( wcSecondArg, L"/s" ) )
			{
				// In standard screensaver mode
				g_bScreensaverMode = true;
			}
			else
			{
				// We can't do a simple comparison for configuration mode
				// as the OS packs the flag AND the HWND into the same argument
				if( wcslen( wcSecondArg ) >= 2 )
				{
					// We've got at least two characters, which means we could have
					// a '/c':
					if( (wcSecondArg[0] == '/') && (wcSecondArg[1] == 'c') )
					{
						g_bConfigurationMode = true;

						// TODO: Parse remainder of argument for the HWND
						if( wcslen( wcSecondArg ) >= 3 )
							g_hConfigParentWindow = (HWND)((DWORD_PTR)_wtoi( &wcSecondArg[2] ) );
					}
				}
			}
		}
	}



	bool ShowConfigurationDialog( )
	{
		ENTER;

		if( g_bConfigurationMode )
		{
			MessageBox( g_hConfigParentWindow, L"This screensaver has no configuration options.", L"'3D Pipes' in Direct3D 10: Screensaver Mode", MB_OK | MB_ICONINFORMATION );
		}

		return g_bConfigurationMode;
	}



	bool RunAsStandardScreensaver( )
	{
		ENTER;

		return g_bScreensaverMode;
	}



	HWND RunAsPreviewScreensaver( )
	{
		ENTER;

		return g_bPreviewMode ? g_hPreviewWindow : 0;
	}
}